import { ref, Ref, onActivated } from "vue";
import { onBeforeRouteLeave } from "vue-router";

export default (pageHeaderWrapper: Ref<HTMLDivElement | undefined>) => {
  const currentScrollTop = ref<number>(0);

  onBeforeRouteLeave(() => {
    currentScrollTop.value = pageHeaderWrapper?.value?.scrollTop || 0;
  });

  onActivated(() => {
    pageHeaderWrapper?.value &&
      (pageHeaderWrapper.value.scrollTop = currentScrollTop.value);
  });
};
